package cn.zwt.wolf.service.position;

import cn.zwt.wolf.domain.position.Position;
import cn.zwt.wolf.repository.position.PositionRepository;
import cn.zwt.wolf.util.JPQLSearchException;
import cn.zwt.wolf.util.SearchUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.NoResultException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangwt on 16-3-13.
 */
@Service
public class PositionService {
    @Autowired
    private PositionRepository positionRepository;
    @Autowired
    private SearchUtil searchUtil;
    private Logger logger = LoggerFactory.getLogger(PositionService.class);

    public Position findPositionById(int id){
        return positionRepository.findOne(id);
    }

    public Position findPositionByCode(String code){
        Position position =null;
        if (code==null)
            return position;
        String hql = "from Position where status = 1 and code=:code";
        Map<String,Object> params= new HashMap<String, Object>();
        params.put("code",code);
        try {
            position = (Position) searchUtil.getSingleResult(hql, params);
        } catch (JPQLSearchException e) {
            logger.error("findPositionByCode error :" + e.getMessage());
        }catch (NoResultException e){
            logger.error("findPositionByCode error :" + e.getMessage());
        }
        return position;
    }
    public Position saveOrUpdate(Position position){
        return positionRepository.save(position);
    }

}
